package com.tengits.react.amap3d.maps;

import android.util.Log;
import android.view.View;

import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.MyLocationStyle;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.tengits.react.amap3d.AMapUtils;

import java.util.HashMap;
import java.util.Map;

public class AMapViewManager extends ViewGroupManager<AMapView> {
    public static final int SET_STATUS = 1;

    @Override
    public String getName() {
        return "AMapView";
    }

    @Override
    public AMapView createViewInstance(ThemedReactContext reactContext) {
        return new AMapView(reactContext);
    }

    @Override
    public void onDropViewInstance(AMapView view) {
        super.onDropViewInstance(view);
        view.onDestroy();
    }

    @Override
    public Map<String, Integer> getCommandsMap() {
      Map result = new HashMap(){{
        put("setStatus", SET_STATUS);
      }};
      return result;
    }

    @Override
    public void receiveCommand(AMapView map, int commandId, ReadableArray args) {
        switch (commandId) {
          case SET_STATUS:
            map.animateTo(args);
            break;
        }
    }

    @Override
    public void addView(AMapView mapView, View child, int index) {
        mapView.add(child);
        super.addView(mapView, child, index);
    }

    @Override
    public void removeViewAt(AMapView parent, int index) {
        parent.remove(parent.getChildAt(index));
        super.removeViewAt(parent, index);
    }

    @Override
    public Map getExportedCustomDirectEventTypeConstants() {
        return MapBuilder.of(
                "onClick", MapBuilder.of("registrationName", "onClick"),
                "onLongClick", MapBuilder.of("registrationName", "onLongClick"),
                "onLocation", MapBuilder.of("registrationName", "onLocation"),
                "onAnimateCancel", MapBuilder.of("registrationName", "onAnimateCancel"),
                "onAnimateFinish", MapBuilder.of("registrationName", "onAnimateFinish"),
                "onStatusChange", MapBuilder.of("registrationName", "onStatusChange"),
                "onStatusChangeComplete", MapBuilder.of("registrationName", "onStatusChangeComplete")
        );
    }

    @ReactProp(name = "locationEnabled")
    public void setMyLocationEnabled(AMapView view, Boolean enabled) {
        view.setLocationEnabled(enabled);
    }

    @ReactProp(name = "showsIndoorMap")
    public void showIndoorMap(AMapView view, Boolean show) {
        view.getMap().showIndoorMap(show);
    }

    @ReactProp(name = "showsIndoorSwitch")
    public void setIndoorSwitchEnabled(AMapView view, Boolean show) {
        view.getMap().getUiSettings().setIndoorSwitchEnabled(show);
    }

    @ReactProp(name = "showsBuildings")
    public void showBuildings(AMapView view, Boolean show) {
        view.getMap().showBuildings(show);
    }

    @ReactProp(name = "showsLabels")
    public void showMapText(AMapView view, Boolean show) {
        view.getMap().showMapText(show);
    }

    @ReactProp(name = "showsCompass")
    public void setCompassEnabled(AMapView view, Boolean show) {
        view.getMap().getUiSettings().setCompassEnabled(show);
    }

    @ReactProp(name = "showsZoomControls")
    public void setZoomControlsEnabled(AMapView view, Boolean enabled) {
        view.getMap().getUiSettings().setZoomControlsEnabled(enabled);
    }

    @ReactProp(name = "showsScale")
    public void setScaleControlsEnabled(AMapView view, Boolean enabled) {
        view.getMap().getUiSettings().setScaleControlsEnabled(enabled);
    }

    @ReactProp(name = "mapLanguage")
    public void setLanguage(AMapView view, int mapLanguage) {
        view.getMap().setMapLanguage((mapLanguage == 1) ?  "en" : "zh_cn");
    }

    @ReactProp(name = "showsLocationButton")
    public void setMyLocationButtonEnabled(AMapView view, Boolean enabled) {
        view.getMap().getUiSettings().setMyLocationButtonEnabled(enabled);
    }

    @ReactProp(name = "showsTraffic")
    public void setTrafficEnabled(AMapView view, Boolean enabled) {
        view.getMap().setTrafficEnabled(enabled);
    }

    @ReactProp(name = "maxZoomLevel")
    public void setMaxZoomLevel(AMapView view, float zoomLevel) {
        view.getMap().setMaxZoomLevel(zoomLevel);
    }

    @ReactProp(name = "minZoomLevel")
    public void setMinZoomLevel(AMapView view, float zoomLevel) {
        view.getMap().setMinZoomLevel(zoomLevel);
    }

    @ReactProp(name = "zoomLevel")
    public void setZoomLevel(AMapView view, float zoomLevel) {
        view.getMap().moveCamera(CameraUpdateFactory.zoomTo(zoomLevel));
    }

    @ReactProp(name = "mapType")
    public void setMapType(AMapView view, int mapType) {
        view.getMap().setMapType(mapType + 1);
    }

    @ReactProp(name = "zoomEnabled")
    public void setZoomGesturesEnabled(AMapView view, Boolean enabled) {
        view.getMap().getUiSettings().setZoomGesturesEnabled(enabled);
    }

    @ReactProp(name = "scrollEnabled")
    public void setScrollGesturesEnabled(AMapView view, Boolean enabled) {
        view.getMap().getUiSettings().setScrollGesturesEnabled(enabled);
    }

    @ReactProp(name = "rotateEnabled")
    public void setRotateGesturesEnabled(AMapView view, Boolean enabled) {
        view.getMap().getUiSettings().setRotateGesturesEnabled(enabled);
    }

    @ReactProp(name = "tiltEnabled")
    public void setTiltGesturesEnabled(AMapView view, Boolean enabled) {
        view.getMap().getUiSettings().setTiltGesturesEnabled(enabled);
    }

    @ReactProp(name = "center")
    public void setCenter(AMapView view, ReadableMap center) {
        view.getMap().moveCamera(CameraUpdateFactory.changeLatLng(AMapUtils.toLatLng(center)));
    }

    @ReactProp(name = "region")
    public void setRegion(AMapView view, ReadableMap region) {
        view.setRegion(region);
    }

    @ReactProp(name = "limitRegion")
    public void setLimitRegion(AMapView view, ReadableMap limitRegion) {
        view.setLimitRegion(limitRegion);
    }

    @ReactProp(name = "tilt")
    public void changeTilt(AMapView view, float tilt) {
        view.getMap().moveCamera(CameraUpdateFactory.changeTilt(tilt));
    }

    @ReactProp(name = "rotation")
    public void changeRotation(AMapView view, float rotation) {
        view.getMap().moveCamera(CameraUpdateFactory.changeBearing(rotation));
    }

    @ReactProp(name = "locationInterval")
    public void setLocationInterval(AMapView view, Integer interval) {
        view.setLocationInterval(interval.longValue());
    }

    @ReactProp(name = "locationStyle")
    public void setLocationStyle(AMapView view, ReadableMap style) {
        view.setLocationStyle(style);
    }

    @ReactProp(name = "locationType")
    public void setLocationStyle(AMapView view, String type) {
        switch (type) {
          case "show":
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);
            break;
          case "locate":
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);
            break;
          case "follow" :
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);
            break;
          case "map_rotate":
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);
            break;
          case "location_rotate" :
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
            break;
          case "location_rotate_no_center":
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
            break;
          case "follow_no_center":
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);
            break;
          case "map_rotate_no_center":
            view.setLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE_NO_CENTER);
            break;
        }
    }
}
